﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EvincePayroll.Data;
using EvincePayroll.Utility;

namespace EvincePayroll.Logic
{
    public class AttendanceLogic : BaseLogic
    {
        public AttendanceLogic(string emailId)
            : base(emailId)
        { }

        public List<AttendanceData> GetAttendanceAdmin(string emailId, string fromDate, string toDate)
        {
            List<AttendanceData> attendanceDataList = null;
            try
            {
                DateTime searchFromDate = DateTime.ParseExact(fromDate, "MM/dd/yyyy", null);
                DateTime searchToDate = DateTime.ParseExact(toDate, "MM/dd/yyyy", null);
                var appOptionList = db.GetAppOptionDataList();
                var employeeList = db.GetEmployeeDataList();
                var attendanceList = db.GetAttendanceDataList().Where(x => x.Date >= searchFromDate && x.Date <= searchToDate).ToList();
                attendanceDataList = (from list in attendanceList
                                      join status in appOptionList on list.Status equals status.ParamId
                                      join employeeData in employeeList on list.EmployeeId equals employeeData.ID
                                      select new AttendanceData
                                      {
                                          AttendanceId = list.AttendanceId,
                                          EmployeeId = list.EmployeeId,
                                          ArrivingTime = list.ArrivingTime,
                                          LeavingTime = GetLeavingTime(list.AttendanceId.ToString()),
                                          Date = list.Date,
                                          StatusString = status.ParamValue,
                                          EmployeeName = employeeData.FirstName + " " + employeeData.LastName,
                                          EmployeeStringId = employeeData.EmployeeId.ToString(),
                                          //TotalHours = GetTotalHours(list.ArrivingTime,db.GetAttendanceLogDataList().Where(x=>x.AttendanceId.Equals(list.AttendanceId)).OrderByDescending(t=>t.LeavingTime).Take(1).Select(t=>t.LeavingTime).FirstOrDefault())
                                          TotalHours = GetTotalHours(list.AttendanceId, list.EmployeeId)
                                      }).ToList<AttendanceData>();






                return attendanceDataList;
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }
            //  return db.GetAttendanceAdmin(emailId,date);


        }

        public List<AttendanceLogData> ShowAttendanceDetail(string email, string attendanceId)
        {
            List<AttendanceLogData> attendanceLogDataList = null;
            try
            {
                var attendanceLogList = db.GetAttendanceLogDataList();
                attendanceLogDataList = (from attendanceLog in attendanceLogList
                                         where attendanceLog.AttendanceId.ToString() == attendanceId
                                         select new AttendanceLogData
                                         {
                                             AttendanceId = attendanceLog.AttendanceId,
                                             EmployeeId = attendanceLog.EmployeeId,
                                             ArrivingTime = attendanceLog.ArrivingTime,
                                             LeavingTime = attendanceLog.LeavingTime,
                                             Remark = attendanceLog.Remark,
                                             TotalHours = attendanceLog.TotalHours,
                                             Date = attendanceLog.Date

                                         }).ToList<AttendanceLogData>();

                return attendanceLogDataList;
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }
        }

        public DateTime GetLeavingTime(string attendanceId)
        {
            DateTime leavingTime = DateTime.MaxValue;
            try
            {

                if (attendanceId != null)
                {
                    var attendance = db.GetAttendanceDataByAttendanceId(attendanceId);
                    leavingTime = attendance.LeavingTime;
                }
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }



            return leavingTime;

        }

        public string GetTotalHours(Guid attendanceId, Guid employeeId)
        {
            string timespan = string.Empty;
            try
            {
                if (attendanceId != null)
                {
                    var attendanceLogList = db.GetAttendanceLogDataList();
                    var attendanceLogListByAttendanceId = attendanceLogList.Where(x => x.AttendanceId.Equals(attendanceId)).ToList();
                    TimeSpan totalTimeSpan = new TimeSpan();
                    foreach (var i in attendanceLogListByAttendanceId)
                    {
                        var totalTime = i.TotalHours.Split(':');
                        TimeSpan t = new TimeSpan(Convert.ToInt16(totalTime[0]), Convert.ToInt16(totalTime[1]), Convert.ToInt16(totalTime[2]));
                        totalTimeSpan = totalTimeSpan.Add(t);
                        
                    }
                   // timespan = totalTimeSpan.Hours.ToString() + ":" + totalTimeSpan.Minutes.ToString() + ":" + totalTimeSpan.Seconds.ToString();
                    timespan = totalTimeSpan.ToString();
                }

            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }



            return timespan;

        }

        public TimeSpan GetTime(DateTime start, DateTime end)
        {
            TimeSpan ts=TimeSpan.MinValue;
                ts = end - start;
            return ts;
        }

        public string GetArrivalLeavingTime(string time)
        {
            string timeString = string.Empty;
            if (time != null)
            {
                timeString = time.Split('.')[0].ToString();
            }
            return timeString;


        }

    }
}
